/*
 * RandomPlaylistFrame.java
 *
 * Created on December 5, 2005, 8:58 AM
 */
package com.bff.mpd.musicjukebox.ui;

import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import com.bff.mpd.musicjukebox.lib.MusicObjects;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.ui.component.BJJButton;
import com.bff.mpd.utils.Utils;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import org.bff.javampd.MPDDatabase;
import org.bff.javampd.MPDPlaylist;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDDatabaseException;
import org.bff.javampd.objects.MPDGenre;

/**
 *
 * @author  bill
 */
public class RandomPlaylistFrame
        extends javax.swing.JFrame {

    private MPDPlaylist playlist;
    private MPDDatabase database;
    private DefaultComboBoxModel genreModel = new DefaultComboBoxModel();
    private DefaultComboBoxModel yearModel = new DefaultComboBoxModel();

    /** Creates new form RandomPlaylistFrame */
    public RandomPlaylistFrame() {
        this.playlist = JukeboxProperties.getInstance().getPlaylist();
        this.database = JukeboxProperties.getInstance().getDatabase();

        initComponents();
        listGenres();
        listYears();
        //set frame nicely in screen
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();

        int heightScreen = d.height / 2;
        int widthScreen = d.width / 2;
        setLocation(widthScreen - (int) (getWidth() / 2), heightScreen - (int) (getHeight() / 2));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        tfPlaylistName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfNumberSongs = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cbStartYear = new javax.swing.JComboBox(yearModel);
        jLabel5 = new javax.swing.JLabel();
        cbEndYear = new javax.swing.JComboBox(yearModel);
        jLabel6 = new javax.swing.JLabel();
        cbGenre = new javax.swing.JComboBox(genreModel);
        jPanel3 = new BJJPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnGenerate = new BJJButton();
        btnCancel = new BJJButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Generate Random Playlist");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Playlist Information"));
        jPanel1.setMinimumSize(new java.awt.Dimension(400, 152));
        jPanel1.setPreferredSize(new java.awt.Dimension(400, 164));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel2.setText("Playlist Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0);
        jPanel1.add(tfPlaylistName, gridBagConstraints);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel3.setText("Number of Songs:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel3, gridBagConstraints);

        tfNumberSongs.setText("100");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0);
        jPanel1.add(tfNumberSongs, gridBagConstraints);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel4.setText("Start Year:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel4, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0);
        jPanel1.add(cbStartYear, gridBagConstraints);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel5.setText("End Year:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel5, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0);
        jPanel1.add(cbEndYear, gridBagConstraints);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel6.setText("Genre:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel6, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0);
        jPanel1.add(cbGenre, gridBagConstraints);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(Utils.getMessage("PLAYLIST_CURRENT_BORDER"))); // NOI18N
        jPanel1.add(jPanel3, new java.awt.GridBagConstraints());

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Generate Random Playlist");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        getContentPane().add(jLabel1, java.awt.BorderLayout.NORTH);

        btnGenerate.setText("Generate");
        btnGenerate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGenerateActionPerformed(evt);
            }
        });
        jPanel2.add(btnGenerate);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jPanel2.add(btnCancel);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGenerateActionPerformed
        this.setCursor(Utils.getHourglassCursor());
        String errMsg = null;
        String name = tfPlaylistName.getText();
        int numSongs = 0;
        String startYear = null;
        String endYear = null;
        String genre = null;
        try {
            if (name == null || name.trim().equals("")) {
                errMsg = "Please enter a playlist name.\n";
            }

            try {
                numSongs = Integer.parseInt(tfNumberSongs.getText());
                if (numSongs < 1) {
                    errMsg += "Must enter a playlist length of at least 1.\n";
                }
            } catch (NumberFormatException nfe) {
                errMsg += "Playlist length must be a number.\n";
            }

            if (cbStartYear.getSelectedIndex() > 0 && cbEndYear.getSelectedIndex() > 0) {
                try {
                    if (Integer.parseInt((String) cbEndYear.getSelectedItem()) < Integer.parseInt((String) cbStartYear.getSelectedItem())) {
                        errMsg += "End year must be greather tha or equal to start year.\n";
                    }
                } catch (Exception e) {
                    //probably nfe
                }
            }

            if (cbStartYear.getSelectedIndex() > 0) {
                startYear = (String) cbStartYear.getSelectedItem();
            }

            if (cbEndYear.getSelectedIndex() > 0) {
                endYear = (String) cbEndYear.getSelectedItem();
            }
            if (errMsg != null) {
                JOptionPane.showMessageDialog(this,
                        errMsg,
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
            } else {
                try {
                    //@TODO fix random playlists with music manager maybe
//                Playlist pl = getPlaylist().createRandomPlaylist(tfPlaylistName.getText(),
//                        startYear,
//                        endYear,
//                        genre,
//                        numSongs);
//                jukeboxPlaylist.setPlaylist(pl);
                    this.dispose();
                } catch (Exception e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(this,
                            "Error creating playlist:\n" + e.getMessage(),
                            "Error",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        } finally {
            this.setCursor(Utils.getNormalCursor());
        }
    }//GEN-LAST:event_btnGenerateActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnGenerate;
    private javax.swing.JComboBox cbEndYear;
    private javax.swing.JComboBox cbGenre;
    private javax.swing.JComboBox cbStartYear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField tfNumberSongs;
    private javax.swing.JTextField tfPlaylistName;
    // End of variables declaration//GEN-END:variables

    private void listGenres() {
        genreModel.removeAllElements();
        genreModel.addElement(new String("Any"));
        List<MPDGenre> genreList;
        try {
            genreList = new ArrayList<MPDGenre>(getDatabase().listAllGenres());
            for (MPDGenre genre : genreList) {
                genreModel.addElement(genre);
            }
        } catch (MPDConnectionException ex) {
            Logger.getLogger(RandomPlaylistFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDDatabaseException ex) {
            Logger.getLogger(RandomPlaylistFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void listYears() {
        yearModel.removeAllElements();
        yearModel.addElement(new String("Any"));
        for (String year : MusicObjects.getYears()) {
            yearModel.addElement(year);
        }
    }

    /**
     * @return the playlist
     */
    public MPDPlaylist getPlaylist() {
        return playlist;
    }

    /**
     * @param playlist the playlist to set
     */
    public void setPlaylist(MPDPlaylist playlist) {
        this.playlist = playlist;
    }

    /**
     * @return the database
     */
    public MPDDatabase getDatabase() {
        return database;
    }

    /**
     * @param database the database to set
     */
    public void setDatabase(MPDDatabase database) {
        this.database = database;
    }
}
